package com.jswlw.controller.admin.base;

import com.jswlw.controller.BaseController;
import com.jswlw.entity.base.Menu;
import com.jswlw.entity.base.Role;
import com.jswlw.entity.base.RoleMenu;
import com.jswlw.service.base.MenuService;
import com.jswlw.service.base.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台角色管理
 * Created by wu on 2017-12-07.
 */
@Controller
@RequestMapping("/admin/role")
public class AdminRoleController extends BaseController {

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    /**
     * 主页
     *
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model) {
        return "role/index";
    }

    /**
     * 角色列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request) {

        Map<String, Object> resultMap = new HashMap<>();
        Map condition = new HashMap<>();
        condition.putAll(parseParams(request));
        //总条数
        resultMap.put("total", roleService.findCount(condition));
        //获取每页数据
        resultMap.put("rows", roleService.findAll(condition));
        return resultMap;
    }

    /**
     * 新增角色
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("role/edit");
        return modelAndView;
    }

    /**
     * 编辑角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info", roleService.findOne(id));
        modelAndView.setViewName("role/edit");
        return modelAndView;
    }

    /**
     * 保存角色
     *
     * @param model
     * @param role
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(Model model, Role role) {
        try {
            if (role.getId() == null) {
                roleService.insert(role);
            } else {
                roleService.update(role);
            }
            return this.successResult("保存成功");
        } catch (Exception e) {
            return this.failResult("保存失败");
        }
    }

    /**
     * 删除角色
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ModelMap delete(Model model, Integer id) {
        try {
            roleService.delete(id);
            return this.successResult(null);
        } catch (Exception e) {
            return this.failResult("删除失败");
        }
    }

    /**
     * 角色授权
     *
     * @param id
     * @return
     */
    @RequestMapping("/auth")
    public ModelAndView auth(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id", id);
        modelAndView.setViewName("role/auth");
        return modelAndView;
    }

    @RequestMapping("/authData")
    @ResponseBody
    public ModelMap authData(Integer id) {
        List<Menu> menuList = menuService.treeList();
        List<Menu> menuListData=new ArrayList<>();
        List<RoleMenu> roleMenuList = roleService.findAuth4Role(id);
        List<Integer> roleMenuIdList = new ArrayList<>();
        roleMenuList.forEach(roleMenu -> {
            roleMenuIdList.add(roleMenu.getMenuId());
        });
        menuList.forEach(menu -> {
            if (roleMenuIdList.contains(menu.getId())) {
                menu.setLAY_CHECKED(true);
            }else{
                menu.setLAY_CHECKED(false);
            }
            menuListData.add(menu);
        });
        return this.successResult(menuListData);
    }

    /**
     * 保存授权
     *
     * @param model
     * @param menus
     * @param id
     * @return
     */
    @RequestMapping(value = "/save_auth", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap saveAuth(Model model, Integer[] menus, Integer id) {
        try {
            roleService.saveAuth(menus, id);
            return this.successResult(null);
        } catch (Exception e) {
            return this.failResult("保存失败");
        }
    }
}
